package com.hctg.forced2sleep.easy_window;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.actor.myandroidframework.utils.ConfigUtils;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.bean.WeekBean;
import com.hctg.forced2sleep.databinding.EasyWindowHourMinuteSelectBinding;

import java.util.Calendar;
import java.util.Date;

/**
 * description: 时间&星期 选择器
 * company    :
 *
 * @author : ldf
 * date       : 2024/11/21 on 11
 * @version 1.0
 */
public class HourMinuteSelectEasyWindow extends EasyWindow2<HourMinuteSelectEasyWindow> implements View.OnClickListener {

    private final EasyWindowHourMinuteSelectBinding viewBinding;
    private OnConfirmClickListener listener;

    public HourMinuteSelectEasyWindow(int startHour, int startMinute, int endHour, int endMinute,
                                      WeekBean weekBean, OnConfirmClickListener listener
    ) {
        super(ConfigUtils.APPLICATION);
        this.listener = listener;
        viewBinding = EasyWindowHourMinuteSelectBinding.inflate(LayoutInflater.from(getContext()), (ViewGroup) getDecorView(), false);
        setContentView(viewBinding.getRoot());
        setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setTag(this.getClass().getName());
        setBackgroundDimAmount(0.6f);

        //周日~周六
        viewBinding.tvSunday.setOnClickListener(this);
        viewBinding.tvMonday.setOnClickListener(this);
        viewBinding.tvTuesday.setOnClickListener(this);
        viewBinding.tvWednesday.setOnClickListener(this);
        viewBinding.tvThursday.setOnClickListener(this);
        viewBinding.tvFriday.setOnClickListener(this);
        viewBinding.tvSaturday.setOnClickListener(this);

        viewBinding.tvCancel.setOnClickListener(this);
        viewBinding.tvSave.setOnClickListener(this);


        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, startHour);
        calendar.set(Calendar.MINUTE, startMinute);
        viewBinding.timePickerStart.setDefaultMillisecond(calendar.getTimeInMillis());
        calendar.set(Calendar.HOUR_OF_DAY, endHour);
        calendar.set(Calendar.MINUTE, endMinute);
        viewBinding.timePickerEnd.setDefaultMillisecond(calendar.getTimeInMillis());

        viewBinding.tvSunday.setSelected(weekBean.isSundaySelect);
        viewBinding.tvMonday.setSelected(weekBean.isMondaySelect);
        viewBinding.tvTuesday.setSelected(weekBean.isTuesdaySelect);
        viewBinding.tvWednesday.setSelected(weekBean.isWednesdaySelect);
        viewBinding.tvThursday.setSelected(weekBean.isThursdaySelect);
        viewBinding.tvFriday.setSelected(weekBean.isFridaySelect);
        viewBinding.tvSaturday.setSelected(weekBean.isSaturdaySelect);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sunday:    //日
            case R.id.tv_monday:    //一
            case R.id.tv_tuesday:   //二
            case R.id.tv_wednesday: //三
            case R.id.tv_thursday:  //四
            case R.id.tv_friday:    //五
            case R.id.tv_saturday:  //六
                view.setSelected(!view.isSelected());
                break;
            case R.id.tv_cancel:
                cancel();
                break;
            case R.id.tv_save:
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date(viewBinding.timePickerStart.getMillisecond()));
                int startHour = calendar.get(Calendar.HOUR_OF_DAY);
                int startMinute = calendar.get(Calendar.MINUTE);
                calendar.setTime(new Date(viewBinding.timePickerEnd.getMillisecond()));
                int endHour = calendar.get(Calendar.HOUR_OF_DAY);
                int endMinute = calendar.get(Calendar.MINUTE);

                WeekBean weekBean = new WeekBean();
                weekBean.isSundaySelect = viewBinding.tvSunday.isSelected();
                weekBean.isMondaySelect = viewBinding.tvMonday.isSelected();
                weekBean.isTuesdaySelect = viewBinding.tvTuesday.isSelected();
                weekBean.isWednesdaySelect = viewBinding.tvWednesday.isSelected();
                weekBean.isThursdaySelect = viewBinding.tvThursday.isSelected();
                weekBean.isFridaySelect = viewBinding.tvFriday.isSelected();
                weekBean.isSaturdaySelect = viewBinding.tvSaturday.isSelected();
                listener.onConfirmClick(startHour, startMinute, endHour, endMinute, weekBean);
                cancel();
                break;
            default:
                break;
        }
    }

    public interface OnConfirmClickListener {
        void onConfirmClick(int startHour, int startMinute, int endHour, int endMinute, @NonNull WeekBean weekBean);
    }
}
